package com.zhike.api.controller.message;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhike.common.dto.CreateSessionDTO;
import com.zhike.common.dto.DeleteSessionDTO;
import com.zhike.common.dto.R;
import com.zhike.common.dto.SessionNoDisturbDTO;
import com.zhike.common.dto.GroupAtUserForSessionDTO;
import com.zhike.common.dto.GroupInfo4Cache;
import com.zhike.common.dto.SessionTopDTO;
import com.zhike.common.enums.ResultCode;
import com.zhike.common.vo.ChatSessionVO;
import com.zhike.common.vo.UserInfo4Cache;
import com.zhike.service.messages.IACKMessageRepository;
import com.zhike.service.messages.IChatSessionService;
import com.zhike.service.system.ISystemService;
import com.zhike.utils.cache.GroupsInfoCacheProvider;
import com.zhike.utils.cache.UsersInfoCacheProvider;
import com.zhike.common.enums.YesNoEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/api/zkim/session")
@Api(tags = "会话模块")
@Slf4j
public class SessionController {

    @Autowired
    private ISystemService systemService;
    @Autowired
    private IChatSessionService chatSessionService;
    @Autowired
    private UsersInfoCacheProvider usersInfoCacheProvider;

    @Autowired
    private GroupsInfoCacheProvider groupsInfoCacheProvider;

    @Autowired
    private IACKMessageRepository aIackMessageRepository;

    @ApiOperation(value = "用户聊天会话列表")
    @PostMapping("loadUserChatSessionPage")
    public R<List<ChatSessionVO>> loadUserChatSessionPage() throws Exception {
        UserInfo4Cache rdsUser = systemService.getUserInfo();
        List<ChatSessionVO> list = chatSessionService.queryUserChatSession(rdsUser.getFakeUid());
        for (ChatSessionVO vo : list) {
            if (StringUtils.isNotBlank(vo.getLastMsgUid())) {
                UserInfo4Cache uc = usersInfoCacheProvider.get(vo.getLastMsgUid());
                vo.setLastMsgNickname(uc == null ? "" : uc.getNickName());
            }
            long count = aIackMessageRepository.unreadCount(rdsUser.getFakeUid(), vo.getSessionId());
            vo.setUnReadCount(count);
            if (StringUtils.isBlank(vo.getToUid()) && StringUtils.isNotBlank(vo.getGroupId())) {
                // 设置群头像
                GroupInfo4Cache gc = groupsInfoCacheProvider.get(vo.getGroupId());
                vo.setAvatarUpdatetime(gc.getAvatarUpdatetime());
            } else {
                // 个人头像
            }
        }

        return R.success(list);
    }

    @ApiOperation(value = "创建会话", notes = "单聊")
    @PostMapping("createSession")
    public R<String> createSession(@Validated @RequestBody CreateSessionDTO dto) {
        UserInfo4Cache uc = systemService.getUserInfo();
        if (uc.getFakeUid().equals(dto.getTargetUid())) {
            return R.rsp(ResultCode.MSG_TARGET_SAME);
        }
        String sessionId = chatSessionService.createSession(uc.getFakeUid(), dto.getTargetUid());
        return R.success(StringUtils.isBlank(sessionId) ? "" : sessionId);
    }

    @ApiOperation(value = "删除会话", notes = "删除单聊或群会话")
    @PostMapping("deleteSession")
    public R<Boolean> deleteSession(@Validated @RequestBody DeleteSessionDTO dto) {
        UserInfo4Cache uc = systemService.getUserInfo();
        return R.success(chatSessionService.deleteSession(uc.getFakeUid(), dto.getUserId(), dto.getGroupId()));
    }

    @ApiOperation(value = "消息免打扰")
    @PostMapping("nodisturb")
    public R<Boolean> noDisturb(@Validated @RequestBody SessionNoDisturbDTO dto) {
        UserInfo4Cache uc = systemService.getUserInfo();
        return R.success(chatSessionService.nodisturb(dto.getSessionId(), uc.getFakeUid(), dto.getAction()));
    }

    @ApiOperation(value = "会话置顶", notes = "设置会话置顶")
    @PostMapping("top")
    public R<Boolean> top(@Validated @RequestBody SessionTopDTO dto) {
        UserInfo4Cache uc = systemService.getUserInfo();
        return R.success(chatSessionService.chatSessionTop(dto.getSessionId(), uc.getFakeUid(), dto.getAction()));
    }

    @ApiOperation(value = "群聊@用户")
    @PostMapping("at")
    public R<Boolean> atUsers(@Validated @RequestBody GroupAtUserForSessionDTO dto) {
        return R.success(chatSessionService.updateSessionAt(dto, YesNoEnum.YES.getValue()));
    }
}
